var ctx=window.daijia.ctx;

$(document).ready(function(){
	
	$("#submit_button").click(function(){
		
		if($("#phone").val()==""){
			$.scojs_message("请输入手机号", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var phone=/(1[3-9]\d{9}$)/;
		if(!phone.test($("#phone").val())){
			$.scojs_message("手机号码格式有误，请重新输入！", $.scojs_message.TYPE_ERROR);
			return false;
		}
		$("#submit_button").css('background-color','#6B6B6B');
		if(activityType=="cash"){
			$("#submit_button").attr("disabled",true);
			$.post(ctx+'/activityweb/phonecoupongift',$("#submit_form").serialize(),function(data){
				if(data.success){
					$("#submit_button").html("已领取");
					$.scojs_message("赠送成功", $.scojs_message.TYPE_OK);
					alert("恭喜你，成功领取到了"+activityMoney+"元现金，赶快去使用吧");
				}
				else{
					$("#submit_button").css('background-color','#F95818');
					$("#submit_button").attr("disabled",false);
                    alert(data.message);
					// window.wxc.xcConfirm(data.message, window.wxc.xcConfirm.typeEnum.confirm);
				}
			});
		}
		else if(activityType=="coupon"){
			$("#submit_button").attr("disabled",true);
			$.post(ctx+'/activityweb/phonecoupongift',$("#submit_form").serialize(),function(data){
				console.debug(data);
				if(data.success){
					var couponIds=[];
					var couponAmounts=[];
					$(data.result.couponIds).each(function(index,element){
						couponIds.push(element);
					});
					$(data.result.couponAmounts).each(function(index,element){
						couponAmounts.push(element);
					});
					$("#submit_button").html("已领取");
					$.scojs_message("赠送成功", $.scojs_message.TYPE_OK);
					window.location=ctx+"/activityweb/success/"+id+"?cids="+couponIds.join('_')+"&cas="+couponAmounts.join('_')+"";
				}
				else{
					$("#submit_button").css('background-color','#F95818');
					$("#submit_button").attr("disabled",false);
					// window.wxc.xcConfirm(data.message, window.wxc.xcConfirm.typeEnum.confirm);
                    alert(data.message);
				}
			});
		}
		
		
		return false;
	});
	
});

